package com.night.icm.controller.base;

import com.night.icm.common.utils.PageDataDTO;
import com.night.icm.common.utils.SimpleDataDTO;
import com.night.icm.model.base.ClassDTO;
import com.night.icm.service.base.service.ClassService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import java.util.List;

/**
 * ClassController
 *
 * @author Liao ZhiYong
 * @date 2019/12/21
 */
@Api(tags = {"班级管理"})
@RestController
@RequestMapping("/api/icm-class")
public class ClassController {

    private final ClassService classService;

    @Autowired
    public ClassController(ClassService classService) {
        this.classService = classService;
    }


    @ApiOperation(value = "分页查询班级信息")
    @GetMapping(value = "")
    public PageDataDTO<ClassDTO> findOnePage(
            @ApiParam(value = "页号，从0开始", required = true, defaultValue = "0") @RequestParam("page") @Min(0) int page,
            @ApiParam(value = "每页记录条数", required = true, defaultValue = "30") @RequestParam("size") @Min(1) @Max(100) int size,
            @ApiParam(value = "排序字段，例如：字段1,asc,字段2,desc") @RequestParam(value = "sort", required = false, defaultValue = "id,desc") String sort,
            @ApiParam(value = "班级编号") @RequestParam(value = "code", required = false) String code,
            @ApiParam(value = "班级名称") @RequestParam(value = "name", required = false) String name,
            @ApiParam(value = "年级") @RequestParam(value = "grade", required = false) String grade,
            @ApiParam(value = "所属学院") @RequestParam(value = "academyId", required = false) String academyId,
            @ApiParam(value = "所属专业") @RequestParam(value = "professionId", required = false) String professionId
    ) {
        return this.classService.findOnePage(page, size, sort, code, name, grade, academyId, professionId);
    }

    @ApiOperation(value = "新增班级")
    @PostMapping(value = "")
    public ClassDTO create(
            @ApiParam(value = "班级信息", required = true) @RequestBody @Validated ClassDTO classDTO
    ) {
        return this.classService.create(classDTO);
    }

    @ApiOperation(value = "删除班级")
    @DeleteMapping(value = "/{id}")
    public void delete(@ApiParam(value = "班级id", required = true) @PathVariable(name = "id") String id) {
        this.classService.delete(id);
    }

    @ApiOperation(value = "更新班级信息")
    @PutMapping(value = "/{id}")
    public ClassDTO update(
            @ApiParam(value = "班级id", required = true) @PathVariable(name = "id") String id,
            @ApiParam(value = "班级信息", required = true) @RequestBody @Validated ClassDTO classDTO
    ) {
        return this.classService.update(id, classDTO);
    }

    @ApiOperation(value = "根据id查找班级信息")
    @GetMapping(value = "/{id}")
    public ClassDTO findById(@ApiParam(value = "班级id", required = true) @PathVariable(name = "id") String id,
                             @ApiParam(value = "是否获取学生", required = true) @RequestParam(value = "isGetStudent") Boolean isGetStudent) {
        return this.classService.findById(id, isGetStudent);
    }

    @ApiOperation(value = "查找所有班级信息，可选择排序")
    @GetMapping(value = "/all")
    public List<ClassDTO> findAll(
            @ApiParam(value = "排序参数") @RequestParam(value = "param", required = false, defaultValue = "id") String param,
            @ApiParam(value = "排序模式: asc/desc") @RequestParam(value = "pattern", required = false, defaultValue = "asc") String pattern
    ) {
        return this.classService.findAll(param, pattern);
    }

    @ApiOperation(value = "检查班级编号是否已存在")
    @GetMapping(value = "/checkCodeExist/{professionId}")
    public SimpleDataDTO<Boolean> checkCodeExist(
            @ApiParam(value = "专业id", required = true) @PathVariable(name = "professionId") String professionId,
            @ApiParam(value = "班级编号", required = true) @RequestParam(value = "code") String code
    ) {
        return this.classService.checkCodeExist(professionId, code);
    }

    @ApiOperation(value = "统计班级数量")
    @GetMapping(value = "/count")
    public Long countClass() {
        return this.classService.countClass();
    }
}